package com.graduation.cdm.funactivity.view.mineview;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.bean.User;

import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;
import static com.graduation.cdm.funactivity.util.xUtilsImageUtils.displayHead;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private User user;
    private ImageView ivUserPhoto;
    private TextView tvUserName;
    private TextView tvSex;
    private TextView tvPhone;
    private TextView tvEmail;
    private RelativeLayout rlModifyPasd;
    private TextView tvEd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        initView();
        getUser();

    }

    private void initView(){
        ivUserPhoto = ((ImageView) findViewById(R.id.iv_user_photo));
        tvUserName = ((TextView) findViewById(R.id.tv_name));
        tvSex = ((TextView) findViewById(R.id.tv_add));
        tvPhone = ((TextView) findViewById(R.id.tv_mobile_phone));
        tvEmail = ((TextView) findViewById(R.id.tv_email));

        rlModifyPasd = ((RelativeLayout) findViewById(R.id.rl_modifyPasd));
        tvEd = ((TextView) findViewById(R.id.tv_ed));

        ivUserPhoto.setOnClickListener(this);
        rlModifyPasd.setOnClickListener(this);
        tvEd.setOnClickListener(this);
    }

    /*private void getUserInfo(){
        Intent intent = getIntent();
        user = ((User) intent.getSerializableExtra("userInfo"));
        System.out.println("==user=="+user);
        if (user!=null){
            displayHead(ivUserPhoto,preUrl+user.getImageUrl());
            tvUserName.setText(user.getUserName().toString());
            tvSex.setText(user.getUserSex().toString());
            tvPhone.setText(user.getUserPhone().toString());
            tvEmail.setText(user.getUserEmail().toString());
        }
    }*/

    private void getUser(){
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        Integer userId = sharedPreferences.getInt("userId",0);
        String userName = sharedPreferences.getString("userName",null);
        String userPassword = sharedPreferences.getString("userPassword",null);
        String userSex = sharedPreferences.getString("userSex",null);
        String userPhone = sharedPreferences.getString("userPhone",null);
        String userEmail = sharedPreferences.getString("userEmail",null);
        String imgUrl = sharedPreferences.getString("imgUrl",null);
        user = new User(userId,userSex,userName,userPassword,userPhone,userEmail,imgUrl);
        if (user!=null){
            displayHead(ivUserPhoto,preUrl+user.getImageUrl());
            tvUserName.setText(user.getUserName().toString());
            tvSex.setText(user.getUserSex().toString());
            tvPhone.setText(user.getUserPhone().toString());
            tvEmail.setText(user.getUserEmail().toString());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_ed:
                Intent intent_user = new Intent(getApplicationContext(), ModifyUserInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userInfo",user);
                intent_user.putExtras(bundle);
                startActivityForResult(intent_user,1);
                break;
            case R.id.iv_user_photo:
                break;
            case R.id.rl_modifyPasd:
                Intent intent = new Intent(getApplicationContext(),ModifyPwdActivity.class);
                startActivityForResult(intent,2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==100){
            getUser();
        }
        if (requestCode==2&&resultCode==200){
            getUser();
        }
    }

}
